package com.swind.fast.modules.app.halali.partner.component;

import java.net.URL;
import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;

import com.swind.fast.common.util.oConvertUtils;

import org.springframework.stereotype.Component;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.swind.fast.modules.app.halali.partner.vo.response.HomeResponse;
import com.swind.fast.modules.app.halali.partner.entity.RestaurantMeal;
import com.swind.fast.modules.app.halali.partner.service.IRestaurantMealService;

import com.swind.fast.modules.app.halali.partner.entity.RestaurantCategory;
import com.swind.fast.modules.app.halali.partner.service.IRestaurantCategoryService;

import com.swind.fast.modules.app.halali.partner.entity.MealCategory;
import com.swind.fast.modules.app.halali.partner.service.IMealCategoryService;
import com.swind.fast.modules.app.halali.partner.entity.RestaurantMealDeal;
import com.swind.fast.modules.app.halali.partner.service.IRestaurantMealDealService;
import com.swind.fast.modules.app.halali.partner.entity.Restaurant;
import com.swind.fast.modules.app.halali.partner.service.IRestaurantService;

import org.springframework.context.ApplicationContext;

import com.swind.fast.common.system.util.JwtUtil;

import cn.hutool.core.date.DateBetween;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateTime;

import com.swind.fast.common.system.vo.DictModel;
import com.swind.fast.modules.system.service.ISysDictService;

import lombok.extern.slf4j.Slf4j;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;

import java.io.File;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;
import com.swind.fast.common.util.CommonUtils;
import java.io.IOException;
import com.swind.fast.modules.app.halali.partner.vo.common.FileUploadVo;

@Slf4j
@Component
public class HalaliUtils {

    @Value(value = "${swind.path.upload}")
    private String uploadPath;

    @Value(value = "${swind.path.preview}")
    private String previewPath;

    public FileUploadVo uploadLocal(MultipartFile mf, String bizPath) {
        FileUploadVo fileVo = new FileUploadVo();
        try {
            String ctxPath = uploadPath;
            String fileName = null;
            File file = new File(ctxPath + File.separator + bizPath + File.separator);
            if (!file.exists()) {
                file.mkdirs();// 创建文件根目录
            }
            String orgName = mf.getOriginalFilename();// 获取文件名
            orgName = CommonUtils.getFileName(orgName);
            if (orgName.indexOf(".") != -1) {
                fileName = orgName.substring(0, orgName.lastIndexOf(".")) + "_" + System.currentTimeMillis() + orgName.substring(orgName.lastIndexOf("."));
            } else {
                fileName = orgName + "_" + System.currentTimeMillis();
            }
            String savePath = file.getPath() + File.separator + fileName;
            File savefile = new File(savePath);
            FileCopyUtils.copy(mf.getBytes(), savefile);
            String dbpath = null;
            if (oConvertUtils.isNotEmpty(bizPath)) {
                dbpath = bizPath + File.separator + fileName;
            } else {
                dbpath = fileName;
            }
            if (dbpath.contains("\\")) {
                dbpath = dbpath.replace("\\", "/");
            }
            String previewPath = this.previewPath + "/" + dbpath;

            // log.info("file: {}", savePath);
            fileVo.setSavePath(dbpath);
            fileVo.setPreviewPath(previewPath);

        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
        return fileVo;
    }

}